
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<span style="font-size:18px;">
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" type="text/css" href="approvalProcess/css/bootstrap.min.css" />
    <link rel="stylesheet" type="text/css" href="approvalProcess/css/commonly-used-show.css" />
    <script type="text/javascript" src="approvalProcess/js/jquery.min.js"></script>
    <script type="text/javascript" src="approvalProcess/js/bootstrap.js" ></script>
    <script type="text/javascript" src="approvalProcess/js/jquery.cookie.js"></script>

    <c:if test="${param.id == 1}">
        <title>添加资讯</title>
    </c:if>

    <c:if test="${param.id != 1}">
        <title>修改资讯</title>
    </c:if>


</head>

<body class="mybody">

<jsp:include page="theTopMenuAdmin.jsp" />

<div class="container-fluid">
    <div class="row">


        <div class="col-md-12">
            <div class="col-md-12">
                    <div class="panel panel-primary">
                        <div class="panel-body">
                            <div class="alert alert-success">


    <div >

        <c:if test="${param.id == 1}">
            <h3 style="text-align: center">添加新闻</h3>
        </c:if>

        <c:if test="${param.id != 1}">
            <h3 style="text-align: center">修改新闻</h3>
        </c:if>


        <hr style="filter: alpha(opacity=100,finishopacity=0,style=3)" width="100%" color="#6f5999" size="3"/>

        <div style="margin-left: 30px; margin-right:30px;">

            <div class="form-group" style="float: left">
                <label ><b style="color: red">*&nbsp</b>新闻标题：</label>
                <input type="text" id="new_name" class="form-control" style="width: 400px;" placeholder="请输入新闻标题" >
            </div>


            <div class="form-group" style="float: left; margin-left: 60px;">
                <label >新闻关键字：</label>
                <input style="width: 350px;" type="text" id="new_keyword" class="form-control edit" placeholder="请输入新闻关键字" >
            </div>

            <div class="form-group" style="float: left; margin-left: 60px;">
                <label ><b style="color: red">*&nbsp</b>新闻发布人：</label>
                <input type="text" id="new_issuer" class="form-control edit" placeholder="请输入新闻发布人" >
            </div>

            <div class="form-group"  style="float: left">
                <label ><b style="color: red">*&nbsp</b>新闻内容：</label>
               <textarea id="new_details" style="height: 400px; width: 600px; resize: none;" class="form-control" placeholder="请输入新闻内容" ></textarea>
            </div>

            <div class="form-group" style="float: left; margin-left: 60px;">
                <label >新闻摘要信息：</label>
               <textarea id="new_digest" style="height: 300px; width: 530px; resize: none;" class="form-control" placeholder="请输入新闻摘要信息" ></textarea>
            </div>

            <div style="clear: both">

            </div>

        </div>

        <div class="text-center" style="margin-top: 50px;">
            <div class="form-group">
                <input id="update_news" type="button" value="确认提交" class="btn btn-primary">
            </div>

            <div>
                <label id="user_null" style="color: red"></label>
            </div>

        </div>

    </div>



                            </div>
                        </div>
                    </div>
                </div>
        </div>



    </div>

 </div>




<script type="text/javascript">
    var userdt = JSON.parse(sessionStorage.getItem("user"));
    var userNull = document.getElementById("user_null");


    if (userdt.result.userName != null){
        $("#usname").text(userdt.result.userName);
    }else {
        $("#usname").text(userdt.result.userLoginName);
    }

    window.onload = function(){
        if ("1" !== "${param.id}"){
            newsInfo();

            $("#update_news").attr("value","确认修改");
            $("#update_news").click(function(){
                updateNews();
            });
        }else {
            $("#update_news").click(function(){
                addNews();
            });
        }
    }

    function newsInfo(){

        var postUrl = "${pageContext.request.contextPath}/selectStripNews";

        var str = {
            "id": "${param.id}"
        };

        $.ajax({
            type:"post",
            url:postUrl,
            headers: {
                "Content-Type":"application/json"
            },
            data:JSON.stringify(str),
            contentType: "application/json; charset=utf-8",
            dataType:"json",
            success:function(data){
                if (data.status == "0"){
                    userNull.innerHTML = data.msg;
                }else {
                    $("#new_name").val(data.result.newsTitle);
                    $("#new_keyword").val(data.result.newsKeywords);
                    $("#new_issuer").val(data.result.newsIssuer);
                    $("#new_digest").val(data.result.newsDescription);
                    $("#new_details").val(data.result.newsContent);
                }

            },
            error:function(xhr,state,errorThrown){
                //alert("发生网络错误，错误码为：" + xhr.status)
                location.href = "${pageContext.request.contextPath}/error";
            }
        });
    }

    function addNews(){
        var newName = $("#new_name").val();
        var newKeyword = $("#new_keyword").val();
        var newIssuer = $("#new_issuer").val();
        var newDigest = $("#new_digest").val();
        var newDetails = $("#new_details").val();

        if ($.isEmptyObject(newName)){
            userNull.innerHTML = "新闻标题不能为空";
            return false;
        }else if ($.isEmptyObject(newIssuer)){
            userNull.innerHTML = "新闻发布人不能为空";
            return false;
        }else if ($.isEmptyObject(newDetails)) {
            userNull.innerHTML = "新闻内容不能为空";
            return false;
        }else {
            userNull.innerHTML = "";
        }

        var postUrl = "${pageContext.request.contextPath}/addNews";
        var str = {
            "newsTitle": newName,
            "newsKeywords": newKeyword,
            "newsIssuer": newIssuer,
            "newsDescription": newDigest,
            "newsContent": newDetails,
            "newsCategory": "0"
        };

        $.ajax({
            type:"post",
            url:postUrl,
            headers: {
                "Content-Type":"application/json"
            },
            data:JSON.stringify(str),
            contentType: "application/json; charset=utf-8",
            dataType:"json",
            success:function(data){
                if (data.status == "0"){
                    userNull.innerHTML = data.msg;
                }else {
                    location.href = "${pageContext.request.contextPath}/andminAllLatestNews";
                }

            },
            error:function(xhr,state,errorThrown){
                //alert("发生网络错误，错误码为：" + xhr.status)
                location.href = "${pageContext.request.contextPath}/error";
            }
        });
    }


    function updateNews(){

        var postUrl = "${pageContext.request.contextPath}/updateNews";
        var str = {
            "id": "${param.id}",
            "newsTitle": $("#new_name").val(),
            "newsKeywords": $("#new_keyword").val(),
            "newsIssuer": $("#new_issuer").val(),
            "newsDescription": $("#new_digest").val(),
            "newsContent": $("#new_details").val()
        };

        $.ajax({
            type:"post",
            url:postUrl,
            headers: {
                "Content-Type":"application/json"
            },
            data:JSON.stringify(str),
            contentType: "application/json; charset=utf-8",
            dataType:"json",
            success:function(data){
                if (data.status == "0"){
                    userNull.innerHTML = data.msg;
                }else {
                    location.href = "${pageContext.request.contextPath}/andminAllLatestNews";
                }

            },
            error:function(xhr,state,errorThrown){
                //alert("发生网络错误，错误码为：" + xhr.status)
                location.href = "${pageContext.request.contextPath}/error";
            }
        });
    }

</script>

</body>
</html>
</span>